// pages/flow/set/set.js
import { examineFlowInfo,setExamineFlow } from '../../../api/app'
import user from '../../../utils/user'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        serveCompanyId: '',
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        await user.waitLogin()
        await examineFlowInfo({
            serveCompanyId: options.id,
            companyId: user.companyId
        }).then(res=>{
            for(const i in res){
                res[i].createUserId = user.userId
                res[i].approverSeq = Number(res[i].approverSeq)
                res[i].ny = Number(res[i].approverSeq) * 60
            }
            this.setData({
                list: res,
                serveCompanyId: options.id
            })
        })
    },

    // 添加审批人
    onAddFlowPeople(){
        wx.navigateTo({
            url: '/pages/team-list/team-list?select=selectPeopleComplete'
        })
    },

    // 添加审批人回调
    selectPeopleComplete(data){
        const list = this.data.list
        for(const i in data){
            let status = 1
            for(const j in list){
                if(list[j].approverId === data[i].id){
                    status = 0
                    break
                }
            }
            if(status)
                list.push({
                    serveCompanyId: this.data.serveCompanyId,
                    companyId: user.companyId,
                    createUserId: user.userId,
                    userName: data[i].name,
                    position: data[i].position,
                    approverId: data[i].id,
                    approverSeq: list.length + 1,
                    ny: (list.length + 1)*60
                })
        }
        this.setData({
            list
        })
    },

    // 修改审批人
    onDeleteFlowPeople(event){
        const list = this.data.list
        const index = event.currentTarget.dataset.index
        const num = list[index].approverSeq
        // 判断是否最后一人
        if(list.length === 1){
            app.toast('审批流中应该至少有一人')
            return 0
        }
        // 删除该流程
        list.splice(index,1)
        // 重新排序
        for(const i in list){
            if(list[i].approverSeq > num){
                list[i].approverSeq -= 1
                list[i].ny = list[i].approverSeq * 60
            }
        }
        this.setData({
            list
        })
    },

    positionChange(event) {
        const index = event.currentTarget.dataset.index
        if(this.data.list[index])
            this.setData({
                ['list[' + index + '].ny']: event.detail.y
            })
    },
    touchend(event) {
        const index = event.currentTarget.dataset.index
        const NoNum = this.data.list[index].approverSeq
        let newNum = Math.floor((Number(this.data.list[index].ny) + 10) / 50)
        console.log('原位置' + NoNum, '新位置' + newNum)
        if(newNum>(this.data.list.length)){
            newNum=this.data.list.length
        }
        if(newNum === 0){
            newNum=1
        }
        if (NoNum === newNum) {
            console.log('位置不变')
            this.setData({
                ['list[' + index + '].approverSeq']: NoNum
            })
        } else {
            if (NoNum > newNum) {
                for (let i = 0; i < this.data.list.length; i++) {
                    const num = this.data.list[i].approverSeq
                    if (num >= newNum && num < NoNum)
                        this.setData({
                            ['list[' + i + '].approverSeq']: num + 1
                        })
                }
            } else {
                for (let i = 0; i < this.data.list.length; i++) {
                    const num = this.data.list[i].approverSeq
                    if (num > NoNum && num <= newNum)
                        this.setData({
                            ['list[' + i + '].approverSeq']: num - 1
                        })
                }
            }
            this.setData({
                ['list[' + index + '].approverSeq']: newNum
            })
        }
    },
    onSubmit(){
        setExamineFlow(this.data.list).then(()=>{
            app.toast('修改成功','success')

            // 通知上一个页面内容已修改
            let pages = getCurrentPages()
            let prevPage = pages[ pages.length - 2 ]
            if(prevPage.changeFlow) prevPage.changeFlow()
        })
    }
})
